layui.use('table', function(){
	var table = layui.table;
	
	//表格数据定义
	table.render({
		elem: '#demo'
		,height: 312
		,url: '/base/label/pageList'
		,height: 'full-100'
		,page: true
		,limit: 10
		,limits: [5,10,20,50]
		,cols: [[
			{title: '序号', width:100, sort: true, fixed: 'left', align: 'center', templet: '#xuhao'}
			,{title: '操作', sort: true, align: 'center', templet: '#btnTemplate'}
			,{field: 'LABELTYPE', title: '标签类别', sort: true, align: 'center'}
			,{field: 'LABELDESC', title: '描述', sort: true, align: 'center'}
			,{title: '状态', width: 100, sort: true, align: 'center', templet: '#statusTemplate'}
			//,{field: 'SEQ', title: '排序号', width: 100, sort: true, align: 'center'}
		]]
	});
	
	//表格事件定义
	table.on('tool(test)', function(obj){
		var data = obj.data; //获得当前行数据
		var layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'del'){ //删除
			layui.layer.confirm('确定删除？', function(index){
				obj.del();
				del(data.ID);
				layer.close(index);
			});
		}
	});

});

//新增或修改
function add(id){
	var ids = "";
	if(id!=undefined && id!=null){
		ids = "?id=" + id;
	}
	var layer = layui.layer;
	layer.open({
		type: 2
		,title: false
		,content: '/base/label/addPage' + ids
		,area: ['800px', '500px']
		,shade: 0.5
		,closeBtn: 1
	});
}

//删除
function del(id){
	$.ajax({
		type: 'get',
		url: '/base/label/del?id=' + id,
		dataType: 'json',
		success: function(result){
			var code = result.code;
			if(code==0){
				layui.layer.msg('删除成功', {icon: 6},function(){
					refresh();//刷新表格
				});	
			}else{
				layui.layer.msg('删除失败', {icon: 5});
			}
		},
		error: function(e){
			layui.layer.msg('网络异常', {icon: 5});
		}
	})
}

//刷新
function refresh(){
	layui.table.reload('demo');
}